package com.yishe.biz.tech;

import com.google.common.collect.ImmutableMap;
import com.yishe.biz.mysql.YsStaffBiz;
import com.yishe.biz.mysql.YsStaffCaseDetailInfoBiz;
import com.yishe.constant.YiSheContant;
import com.yishe.context.BaseContextHandler;
import com.yishe.entites.mysql.CaseTypeMaintain;
import com.yishe.entites.mysql.YsCaseInfo;
import com.yishe.entites.mysql.YsStaff;
import com.yishe.entites.vo.YsStaffCaseDetailVo;
import com.yishe.entites.vo.YsStaffCaseVo;
import com.yishe.mapper.mysql.CaseTypeMaintainMapper;
import com.yishe.mapper.mysql.YsCaseInfoMapper;
import com.yishe.mapper.mysql.YsStaffCaseDetailInfoMapper;
import com.yishe.mapper.mysql.YsStaffCaseMapper;
import com.yishe.utils.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author : chusifeng
 * @version :1.0
 * @date :Create in 2021/4/8 21:48
 * @description: 教师/管理员管理service
 * @package com.yishe.biz
 * @className TechBiz
 */
@Service
@Slf4j
public class TechBiz {
    @Value("${yishe.user.token.expireTime:1800}")
    private int tokenExpireTime;
    @Resource
    private YsStaffBiz ysStaffBiz;
    @Resource
    private YsStaffCaseDetailInfoBiz ysStaffCaseDetailInfoBiz;
    @Resource
    private YsStaffCaseMapper ysStaffCaseMapper;
    @Resource
    private YsCaseInfoMapper ysCaseInfoMapper;
    @Resource
    private CaseTypeMaintainMapper caseTypeMaintainMapper;


    /**
     * 获取本人名下的所有已分配的教学课程
     *
     * @param account
     * @return
     */
    public List<YsStaffCaseDetailVo> getCaseListDetail(String account) {
        if (StringUtils.isBlank(account)) {
            YsStaff ysStaff = getCurrentStaff();
            //为空应该是超级管理员查询所有，但是发现权限不是
            if (!YiSheContant.Position_Type.Super.getCode().equals(ysStaff.getPosition())) {
                return Collections.EMPTY_LIST;
            }
        }
        List<YsStaffCaseDetailVo> resultList = ysStaffCaseDetailInfoBiz.getCaseListDetail(ImmutableMap.of("account", account));
        resultList.forEach(item -> item.setDealName(YiSheContant.Case_Status.getName(item.getIsDeal())));
        return resultList;
    }

    /**
     * 获取本人课程/所有课程信息
     *
     * @param account
     * @return
     */
    public List<Map<String, Object>> getCaseList(String account) {
        List<Map<String, Object>> res = new ArrayList<>();
        if (StringUtils.isBlank(account)) {
            YsStaff ysStaff = getCurrentStaff();
            //为空应该是超级管理员查询所有，但是发现权限不是
            if (!YiSheContant.Position_Type.Super.getCode().equals(ysStaff.getPosition())) {
                return Collections.EMPTY_LIST;
            }
            res =  ysCaseInfoMapper.getListInfo(Collections.EMPTY_MAP);
        } else {
            res = ysStaffCaseMapper.getCaseListByAccount(account);
        }
        return res;
    }

    /**
     * 获取课程信息
     *
     * @param param
     * @return
     */
    public YsStaffCaseVo getCaseObject(Map<String, String> param) {
        String caseNo = param.get("caseNo");
        String isSuper = param.get("isSuper");

        if ("A".equals(isSuper)) {
            param.remove("account");
            //查询该课程所有分配信息
        }
        YsStaffCaseVo result = new YsStaffCaseVo();
        YsCaseInfo ysCaseInfo = new YsCaseInfo();
        ysCaseInfo.setCaseNo(caseNo);
        ysCaseInfo = ysCaseInfoMapper.selectOne(ysCaseInfo);
        BeanCopyUtils.copy(result,ysCaseInfo);
        List<YsStaffCaseVo> ysStaffCaseVos = this.getYsStatffCaseListByParam(param);
        //根据该课程获取类型配置
        CaseTypeMaintain typeMaintain = new CaseTypeMaintain();
        typeMaintain.setCaseType(ysCaseInfo.getCaseType());
        typeMaintain = caseTypeMaintainMapper.selectOne(typeMaintain);
        for(YsStaffCaseVo ysStaffCaseVo : ysStaffCaseVos) {
            ysStaffCaseVo.setCaseSale(typeMaintain.getCaseSale());
            ysStaffCaseVo.setCaseSaleDz(typeMaintain.getCaseSaleDz());
        };
        result.setAccounts(ysStaffCaseVos);
        return result;
    }
    /**
     * 查询某一个课程的分配信息
     */
    public List<YsStaffCaseVo> getYsStatffCaseListByParam(Map<String, String> param){
        //查询该课程分配的信息
        return ysStaffCaseMapper.getStaffCaseByCaseNo(param);
    }
    /**
     * 获取当前登录员工信息
     *
     * @return
     */
    private YsStaff getCurrentStaff() {
        String loginAccount = BaseContextHandler.getUserCode();
        YsStaff ysStaff = new YsStaff();
        ysStaff.setAccount(loginAccount);
        ysStaff = ysStaffBiz.selectOne(ysStaff);
        return ysStaff;
    }


}
